<?php

  require_once '../functions.php';
  
  xiu_get_current_user();
  
  // 接收筛选参数
  // ==================================
  
  // 存储sql 语句中的 where 条件
  $where = '1 = 1';
  $search = '';
  
  // 分类筛选
  
  if(isset($_GET['category']) && $_GET['category'] !== 'all'){
    $where .= ' and posts.category_id = ' . $_GET['category'];
    $search .='&category='. $_GET['category']; 
  }
  
  if(isset($_GET['status']) && $_GET['status'] !== 'all'){
    $where .=" and posts.status = '{$_GET['status']}' ";
    $search .= '&status='.$_GET['status'];
  }

  // 处理分页参数
  // =========================================
  
  $size = 20 ;
  $page = empty($_GET['page']) ? 1 : (int)$_GET['page'];
  // 必须 >= 1 && <= 总页数
  
  // $page = $page < 1 ? 1 : $page;
  if ($page < 1) {
      // 跳转到第一页
      header('Location: /admin/posts.php?page=1'.$search );
    }
  
  // 只要是处理分页功能一定会用到最大的页码数

  //总条数
  $total_count = (int)xiu_fetch_one("select count(1) as count from posts
  inner join categories on posts.category_id = categories.id
  inner join users on posts.user_id = users.id
  where {$where};")['count'];

  //最大的页码数
  $total_pages = (int)ceil($total_count / $size);
  
  // $page = $page > $total_pages ? $total_pages : $page;
  if ($page > $total_pages) {
    // 跳转到第最后页
    header('Location: /admin/posts.php?page=' . $total_pages .$search );
  }
  
  // 获取全部数据
  // ===================================
  
  // 计算出越过多少条
  $offset = ($page - 1) * $size;
  
  // 获取全部类型的数据
  $posts = xiu_fetch_all("select
    posts.id,
    posts.title,
    users.nickname as user_name,
    categories.name as category_name,
    posts.created,
    posts.status
  from posts
  inner join categories on posts.category_id = categories.id
  inner join users on posts.user_id = users.id
  where {$where}
  order by posts.created desc
  limit {$offset}, {$size};");
  
  // 查询全部的分类数据
  $categories = xiu_fetch_all('select * from categories;');
    
  // 处理分页页码
  // ===============================
  
  //上一页下一页功能所需要的变量
  
  $visiables = 5;
  
  // 计算最大和最小展示的页码
  $begin = $page - ($visiables - 1) / 2;
  $end = $begin + $visiables - 1;
  
  // 重点考虑合理性的问题
  // begin > 0  end <= total_pages
  $begin = $begin < 1 ? 1 : $begin; // 确保了 begin 不会小于 1
  $end = $begin + $visiables - 1; // 因为上一行可能导致 begin 变化，这里同步两者关系
  $end = $end > $total_pages ? $total_pages : $end; // 确保了 end 不会大于 total_pages
  $begin = $end - $visiables + 1; // 因为上一行可能改变了 end，也就有可能打破 begin 和 end 的关系
  $begin = $begin < 1 ? 1 : $begin; // 确保不能小于 1
  
  // 处理数据格式转换
  // ===========================================
  
  /**
   * 转换状态显示
   * @param  string $status 英文状态
   * @return string         中文状态
   */
  function convert_status ($status) {
    $dict = array(
      'published' => '已发布',
      'drafted' => '草稿',
      'trashed' => '回收站'
    );
    return isset($dict[$status]) ? $dict[$status] : '未知';
  }
  
  /**
   * 转换时间格式
   * @param  [type] $created [description]
   * @return [type]          [description]
   */
  function convert_date ($created) {
    // => '2017-07-01 08:08:00'
    // 如果配置文件没有配置时区
    date_default_timezone_set('PRC');
    $timestamp = strtotime($created);
    return date('Y年m月d日<b\r>H:i:s', $timestamp);
  }

?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>Posts &laquo; Admin</title>
  <link rel="stylesheet" href="/static/assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="/static/assets/vendors/font-awesome/css/font-awesome.css">
  <link rel="stylesheet" href="/static/assets/vendors/nprogress/nprogress.css">
  <link rel="stylesheet" href="/static/assets/css/admin.css">
  <script src="/static/assets/vendors/nprogress/nprogress.js"></script>
</head>
<body>
  <script>NProgress.start()</script>

  <div class="main">
    <?php include 'inc/navbar.php'; ?>

    <div class="container-fluid">
      <div class="page-title">
        <h1>所有文章</h1>
        <a href="post-add.html" class="btn btn-primary btn-xs">写文章</a>
      </div>
      <!-- 有错误信息时展示 -->
      <!-- <div class="alert alert-danger">
        <strong>错误！</strong>发生XXX错误
      </div> -->
      <div class="page-action">
        <!-- show when multiple checked -->
        <a class="btn btn-danger btn-sm" href="/admin/posts-delete.php" style="display: none" id="btn_delete">批量删除</a>
        <form class="form-inline" action="<?php echo $_SERVER['PHP_SELF']; ?>">
          <select name="category" class="form-control input-sm">
            <option value="all">所有分类</option>
            <?php foreach ($categories as $item): ?>
            <option value="<?php echo $item['id']; ?>"<?php echo isset($_GET['category']) && $_GET['category'] == $item['id'] ? ' selected' : '' ?>>
              <?php echo $item['name']; ?>
            </option>
            <?php endforeach ?>
          </select>
         <select name="status" class="form-control input-sm">
            <option value="all">所有状态</option>
            <option value="drafted" <?php echo isset($_GET['status']) && $_GET['status'] == 'drafted' ? ' selected' : '' ?>>草稿</option>
            <option value="published" <?php echo isset($_GET['status']) && $_GET['status'] == 'published' ? ' selected' : '' ?>>已发布</option>
            <option value="trashed" <?php echo isset($_GET['status']) && $_GET['status'] == 'trashed' ? ' selected' : '' ?>>回收站</option>
          </select>
          <button class="btn btn-default btn-sm">筛选</button>
        </form>
         <ul class="pagination pagination-sm pull-right">
          <li><a href="?page=<?php echo ($page - 1).$search  ?>">上一页</a></li>
          <?php for($i = $begin; $i <= $end ; $i++): ?>
            <li <?php echo $i === $page ? 'class="active"' : "" ;?>><a href="?page=<?php echo $i.$search  ;?>"><?php echo $i  ;?></a></li>
          <?php endfor ?>
          <li><a href="?page=<?php echo ($page + 1).$search ?>">下一页</a></li>
        </ul>
      </div>
      <table class="table table-striped table-bordered table-hover">
        <thead>
          <tr>
            <th class="text-center" width="40"><input type="checkbox" id="theadBox"></th>
            <th>标题</th>
            <th>作者</th>
            <th>分类</th>
            <th class="text-center">发表时间</th>
            <th class="text-center">状态</th>
            <th class="text-center" width="100">操作</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($posts as $item): ?>
          <tr>
            <td class="text-center"><input type="checkbox" data-id="<?php echo $item['id']?>"></td>
            <td><?php echo $item['title']; ?></td>
            <!-- <td><?php // echo get_user($item['user_id']); ?></td>
            <td><?php // echo get_category($item['category_id']); ?></td> -->
            <td><?php echo $item['user_name']; ?></td>
            <td><?php echo $item['category_name']; ?></td>
            <td class="text-center"><?php echo convert_date($item['created']); ?></td>
            <!-- 一旦当输出的判断或者转换逻辑过于复杂，不建议直接写在混编位置 -->
            <td class="text-center"><?php echo convert_status($item['status']); ?></td>
            <td class="text-center">
              <a href="/admin/post-add.php?id=<?php echo $item['id'] ;?>" class="btn btn-default btn-xs">编辑</a>
              <a href="/admin/posts-delete.php?id=<?php echo $item['id'] ;?>" class="btn btn-danger btn-xs">删除</a>
            </td>
          </tr>
          <?php endforeach ?>
        </tbody>
      </table>
    </div>
  </div>

  <?php $current_page = 'posts'; ?>
  <?php include 'inc/sidebar.php'; ?>

  <script src="/static/assets/vendors/jquery/jquery.js"></script>
  <script src="/static/assets/vendors/bootstrap/js/bootstrap.js"></script>
  <script>
  //全部删除功能
    $(function($){
    var $tbodyChecked = $('tbody input:checkbox');
    var $btnDelete = $('#btn_delete');
    var $thCheckbox = $('#theadBox');
    var allCheckbox = [];

    $tbodyChecked.on('change',function(){
      var $id = $(this).data('id');
      if($(this).prop('checked')){
        allCheckbox.push($id);
      }else{
        allCheckbox.splice(allCheckbox.indexOf($id),1);
      }

      allCheckbox.length ? $btnDelete.fadeIn() : $btnDelete.fadeOut();
      $thCheckbox.prop('checked',$tbodyChecked.length == $tbodyChecked.filter(':checked').length);
      $btnDelete.prop('search','?id='+allCheckbox);

    });

    $thCheckbox.on('change',function(){
      var id='';
      $tbodyChecked.prop('checked',$(this).prop('checked'));
      if($(this).prop('checked')){
        $btnDelete.fadeIn();
        $tbodyChecked.prop('checked',$(this).prop('checked'));
        $tbodyChecked.each(function(inx,ele){
          id = $(ele).data('id');
          allCheckbox.push(id);
          console.log(allCheckbox);
        });
      }else{
        $btnDelete.fadeOut();
        $tbodyChecked.prop('checked',$(this).prop('checked'));
        $tbodyChecked.each(function(inx,ele){
          id = $(ele).data('id');
          allCheckbox.splice(allCheckbox.indexOf(id),1);
          console.log(allCheckbox);
        });
      }
      $btnDelete.prop('search','?id='+allCheckbox)
    });
  });
  </script>
  <script>NProgress.done()</script>
</body>
</html>
